import {Injectable} from "@angular/core";
import {HttpClient, HttpParams} from "@angular/common/http";
import {Observable} from "rxjs/Observable";
import * as moment from 'moment';

@Injectable()
export class RosterListProvider {

    constructor(private http: HttpClient) {
    }

    getRosterList(params, notLoading=0) {
        let url = `/followstate/v1/app/agentCampaignCust/list?`;
        for (let key in params) {
            if ((key === 'starttime' || key === 'startTakeTime' )) {
                if (params[key]) {
                    params[key] = moment(params[key]).unix();
                } else {
                    delete params[key];
                }
            }
            if (key === 'endtime'|| key === 'endTakeTime') {
                if (params[key]) {
                    params[key] = moment(params[key] + ' 23:59:59').unix();
                } else {
                    delete params[key];
                }
            }
        }
        let parseParams = new HttpParams({
            fromObject: params
        });
        url += parseParams.toString();
        return this.http.get(url);
    }

    switchRosterStatus(params): Observable<any> {
        return this.http.post(`/followstate/v1/app/update`, params, {responseType: 'text'});
    }

    getRosterListById (id,params,notLoading=0) {
        let url = `/followstate/v1/app/agentCampaignCust/singleCampaignlist?id=${id}&`;

        params.startTakeTime = params.startTakeTime ? moment(params.startTakeTime).unix() : 0;
        params.endTakeTime = params.endTakeTime ? moment(params.endTakeTime + ' 23:59:59').unix() : 0;
        let parseParams = new HttpParams({
            fromObject: params
        });
        url += parseParams.toString();
        return this.http.get(url);
    }
}


